<?php
namespace Admin\Controller;
use Think\Controller;
class TestQuestionsController extends BaseControlle {
    public function __construct()
    {
        parent::__construct();
    }

    public function NewTopicUI(){
        $assignmentid = I('get.assignmentid');
        if (!$assignmentid){
            //输出报错信息
            $this->PromptInformation('请传入题号');
        }
        $examination = M('examination');
        $ret_examination = $examination->select();

        $exam_question_number     = M('exam_question_number');
        $ret_exam_question_number = $exam_question_number->where(array('exam_assignment_id'=>$assignmentid))->select();

        $this->assign('assignmentid',$assignmentid);
        $this->assign('ret_examination',$ret_examination);
        $this->assign('ret_question_number',$ret_exam_question_number);
        $this->display();
    }

    public function SelectPoticLibrary(){
        $examinationid = I('post.examinationid',0,'intval');
        $page          = I('post.page',0,'intval');
        $limit         = I('post.limit',0,'intval');
        if (!$examinationid){
            $this->ajaxReturn(array('code'=>500,'msg'=>'传入的数据不正确'));
        }
        $job_library = M('job_library');
        $ret_job_library_count = $job_library->where(array('examinationid'=>$examinationid))->count();

        $page   = DBPage($ret_job_library_count,$limit,$page);

        $ret_job_library = $job_library
            ->where(array('examinationid'=>$examinationid,'status'=>1))
            ->field(array('id','sequence','body','type','knowledge'))
            ->limit($limit)
            ->page($page['page'])
            ->order('id ASC')
            ->select();
        foreach ($ret_job_library as $item=>$value){
            $ret_job_library[$item]['body'] = strip_tags(htmlspecialchars_decode($value['body']));
        }
        $this->ajaxReturn(array('code'=>0,'msg'=>'','count'=>$ret_job_library_count,'data'=>$ret_job_library));
    }
    public function Edit(){
        $this->display();
    }

    public function ManageSelect(){

        $assignmentid = I('post.assignmentid');
        $exam_topic   = M('exam_topic');
        //$job_library  = M('job_library');

        $DB_PREFIX = C('DB_PREFIX');

        $page   =      I('post.page',0,'intval');
        $limit  =     I('post.limit',0,'intval');
        $count  =      $exam_topic->where(array('exam_assignment_id'=>$assignmentid))->count();
        $page   = DBPage($count,$limit,$page);

        $exam_topic_list = $exam_topic
            ->where("{$DB_PREFIX}exam_topic.exam_assignment_id='$assignmentid'")
            ->join("__JOB_LIBRARY__ ON __JOB_LIBRARY__.id=__EXAM_TOPIC__.job_library_id",'LEFT')
            ->join("__EXAM_QUESTION_NUMBER__ ON __EXAM_QUESTION_NUMBER__.id=__EXAM_TOPIC__.title_number",'LEFT')
            ->limit($limit)
            ->page($page['page'])
            ->order("{$DB_PREFIX}exam_question_number.sequence ASC,{$DB_PREFIX}exam_topic.sequence ASC,{$DB_PREFIX}exam_topic.number ASC")
            ->field("{$DB_PREFIX}job_library.*,{$DB_PREFIX}exam_topic.id AS topicid,{$DB_PREFIX}exam_topic.exam_assignment_id,{$DB_PREFIX}exam_topic.id as topicTitleNumber,{$DB_PREFIX}exam_topic.sequence,{$DB_PREFIX}exam_topic.score,{$DB_PREFIX}exam_topic.number,left({$DB_PREFIX}exam_question_number.title_explain,6) as title_explain")
            ->select();

        foreach ($exam_topic_list as $item=>$value) {
            $exam_topic_list[$item]['body'] = strip_tags(htmlspecialchars_decode($value['body']));
        }

        $this->ajaxReturn(array('code'=>0,'msg'=>'','count'=>$count,'data'=>$exam_topic_list));
    }


    public function Manage(){
        $assignmentid = I('get.assignmentid',0,'intval');
        $exam_question_number = M('exam_question_number');
        if (!$assignmentid){
            $this->PromptInformation('请正确传入数据');
        }
        $ret_exam_question_number = $exam_question_number->where(array('exam_assignment_id'=>$assignmentid))->select();

        $this->assign('assignmentid',$assignmentid);
        $this->assign('exam_question_number',$ret_exam_question_number);
        $this->display();
    }

    public function Delete(){
        $topicid = I('post.topicid',array());

        if(!count($topicid)){
            $this->ajaxReturn(array('code'=>500,'msg'=>'请传入删除条件'));
        }

        $exam_topic = M('exam_topic');
        $is_true  = false;
        $is_false = false;
        $is_false_str = array();
        foreach ($topicid as $item=>$value){
            $ret_exam_topic = $exam_topic->where(array('id'=>$value))->delete();
            if ($ret_exam_topic){
                $is_true = true;
            }else{
                $is_false = true;
                $is_false_str[] = $value;
            }
        }
        if ($is_true && !$is_false){
            $this->ajaxReturn(array('code'=>200,'msg'=>'删除成功'));
        }elseif($is_true && $is_false){
            $this->ajaxReturn(array('code'=>200,'msg'=>'部分删除成功，失败:'.implode(',',$is_false_str)));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'删除全部失败'));
        }
    }
    //大题号管理
    public function TitleNumberManage(){
        $assignmentid = I("get.assignmentid",0,'intval');
        if (!$assignmentid){
            $this->error('没传入正确的数据');
        }
        $this->assign('assignmentid',$assignmentid);
        $this->display();
    }
    //添加大题号UI
    public function AddQIDUI(){
        $assignmentid = I("get.assignmentid",0,'intval');
        if (!$assignmentid){
            $this->error('没传入正确的数据');
        }
        $this->assign('assignmentid',$assignmentid);
        $this->display();
    }
    //修改大题号UI
    public function EditAddQIDUI(){
        $examquestnum = I("get.examquestnum",0,'intval');
        if (!$examquestnum){
            $this->error('没传入正确的数据');
        }
        $exam_question_number = M('exam_question_number');
        $ret_exam_question_number = $exam_question_number->where(array('id'=>$examquestnum))->find();
        if (!$ret_exam_question_number){
            $this->error('题号库没有相关信息存在');
        }

        $this->assign('examquestnum',$examquestnum);
        $this->assign('exam_question_number',$ret_exam_question_number);
        $this->display();
    }

    //新建大题
    public function NewTitleNumber(){
        $assignmentid     = I("post.assignmentid",0,'intval');
        $AddTitleSequence = I("post.AddTitleSequence",0,'intval');
        $TitleExplain     = I("post.TitleExplain","");
        if ($assignmentid < 1){
            $this->ajaxReturn(array('code'=>500,'msg'=>'题号传入错误'));
        }
        $exam_question_number = M('exam_question_number');
        $db = array(
            'sequence'=>$AddTitleSequence,
            'title_explain'=>$TitleExplain,
            'exam_assignment_id'=>$assignmentid
        );
        $ret_exam_question_number = $exam_question_number->data($db)->add();
        if ($ret_exam_question_number){
            $this->ajaxReturn(array('code'=>200));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'题目添加失败'));
        }
    }
    //更改大题号信息
    public function UpdataTitleNumber(){
        $id                   = I("post.examquestnum",0,'intval');
        $sequence             = I("post.sequence",0,'intval');
        $title_explain        = I("post.title_explain","");
        $exam_question_number = M('exam_question_number');

        $db = array(
            'sequence'=>$sequence,
            'title_explain'=>$title_explain
        );
        $ret_exam_question_number = $exam_question_number->where(array('id'=>$id))->save($db);
        if ($ret_exam_question_number){
            $this->ajaxReturn(array('code'=>200));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'更新失败'));
        }
    }
    //查询题号
    public function SelectTitleNumber(){
        $assignmentid     = I("post.assignmentid",0,'intval');
        $exam_question_number = M('exam_question_number');
        $count_exam_question_number = $exam_question_number->where(array('exam_assignment_id'=>$assignmentid))->count();
        $ret_exam_question_number = $exam_question_number->where(array('exam_assignment_id'=>$assignmentid))->select();

        foreach ($ret_exam_question_number as $k=>$v){
            $ret_exam_question_number[$k]['title_explain'] = strip_tags(htmlspecialchars_decode($v['title_explain']));
        }
        $ret = array(
            'code' => 0,
            'msg' => "",
            'count' => $count_exam_question_number,
            'data' => $ret_exam_question_number
        );

        if ($ret_exam_question_number){
            $this->ajaxReturn($ret);
        }else{
            $ret['code'] = 1;
            $ret['msg'] = "数据查询失败";
            $this->ajaxReturn($ret);
        }

    }

    //删除大题号
    public function DelectTitleNumber(){
        //删除大题号，注意删除试卷列表的相同答题号的题目删除
        $questionnumber       = I("post.questionnumber",0,'intval');
        $exam_question_number = M('exam_question_number');
        $exam_topic           = M('exam_topic');
        $error_msg                  = '';

        if ($questionnumber < 1){
            $this->ajaxReturn(array('code'=>500,'msg'=>"请传入正确的题号编码"));
        }

        //检查题号是否存在
        $ret_exam_question_number = $exam_question_number->where(array('id'=>$questionnumber))->find();
        if (!$ret_exam_question_number){
            $this->ajaxReturn(array('code'=>500,'msg'=>'题号不存在'));
        }

        //查询关联的题目数量
        $count_exam_topic = $exam_topic->where(array('title_number'=>$ret_exam_question_number['id']))->count();
        if ($count_exam_topic != 0){

            //删除题库关联的题目
            $ret_exam_topic = $exam_topic->where(array('title_number'=>$ret_exam_question_number['id']))->delect();
            if (!$ret_exam_topic){
                $error_msg +=  '关联题目删除失败';
            }
        }

        //删除大题号
        $ret_del_exam_question_number = $exam_question_number->where(array('id'=>$questionnumber))->delete();
        if (!$ret_del_exam_question_number){
            $this->ajaxReturn(array('code'=>500,'msg'=>'大题号删除失败 '.$error_msg));
        }else{
            $this->ajaxReturn(array('code'=>200,'msg'=>$error_msg));
        }
    }




    //添加题目到大题号
    public function AddQID(){
        $data = I('post.data',array());
        $assignmentid = I('post.assignmentid',0,'intval');
        $selectQid = I('post.selectQid',0,'intval');
        $job_library = M('job_library');
        $exam_topic = M('exam_topic');


        $ret_exam_topic_error = array();
        $ret_exam_topic       = true;
        //批量添加考试题目
        foreach ($data as $item=>$value){
            $db = array(
                'exam_assignment_id'=>$assignmentid,
                'job_library_id'=>intval($value['id']),
                'sequence'=>intval($value['sequence']),
                'number'=>$value['sequence'],
                'title_number'=>$selectQid
            );
            $ret = $exam_topic->data($db)->add();
            if(!$ret){
                $ret_exam_topic_error[] = $value;
                $ret_exam_topic = false;
            }
        }
        if ($ret_exam_topic){
            $this->ajaxReturn(array('code'=>200,'msg'=>'添加成功'));
        }else{
            $this->ajaxReturn(array('code'=>200,'msg'=>'试卷创建成功，但是批量添加题目似乎出了些问题'));
        }
    }

    //修改题目关联大题号
    public function UpdateTopicTitleNumber(){
        $tpoicId      = I('post.tpoicId');
        $assignmentid = I('post.assignmentid',0,'intval');
        $TitleName    = I('post.TitleName',0,'intval');
        $exam_topic   = M('exam_topic');
        $is_true  = false;
        $is_false = false;
        foreach ($tpoicId as $key=>$value){
            if(is_numeric($value)){
                $ret_exam_topic = $exam_topic->where(array('id'=>$value,'exam_assignment_id'=>$assignmentid))->save(array('title_number'=>$TitleName));
                if($ret_exam_topic){
                    $is_true = true;
                }else{
                    $is_false = true;
                }
            }
        }

        if ($is_true && $is_false){
            $this->ajaxReturn(array('code'=>200,'msg'=>'部分成功，部分失败！'));
        }elseif (!$is_true && $is_false){
            $this->ajaxReturn(array('code'=>500,'msg'=>'全部更新失败'));
        }elseif($is_true && !$is_false){
            $this->ajaxReturn(array('code'=>200,'msg'=>'全部更新成功'));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'所有操作都未执行'));
        }
    }

    public function UpdataSequenceAndnumber(){
        $topicid  = I('post.topicid',0,'intval');
        $sequence = I('post.sequence',0,'intval');
        $number   = I('post.number',0,'intval');
        $score    = I('post.score',0,'intval');

        $exam_topic = M('exam_topic');

        if($sequence){
            $ret_exam_topic = $exam_topic->where(array('id'=>$topicid))->save(array('sequence'=>$sequence));
        }elseif ($number){
            $ret_exam_topic = $exam_topic->where(array('id'=>$topicid))->save(array('number'=>$number));
        }elseif ($score){
            $ret_exam_topic = $exam_topic->where(array('id'=>$topicid))->save(array('score'=>$score));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'传入的数据不正确'));
        }

        if ($ret_exam_topic){
            $this->ajaxReturn(array('code'=>200,'msg'=>'数据更新成功'));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'数据更新失败'));
        }
    }


}